//
//  ChanpinliebiaoViewController.m
//  tianjin
//
//  Created by 鞠超 on 16/8/8.
//  Copyright © 2016年 jc. All rights reserved.
//

#import "ChanpinliebiaoViewController.h"

#import "ChanpinliebiaoCollectionViewCell.h"
#import "ChanpinxiangqingViewController.h"

@interface ChanpinliebiaoViewController () <UICollectionViewDelegateFlowLayout, UICollectionViewDataSource>

@property (nonatomic, strong) UIView * view_headView;
//3个btn
//销量
@property (nonatomic, strong) UIButton * btn_1;
@property (nonatomic, strong) UILabel * name_1;
@property (nonatomic, strong) UIImageView * image_1;
//价格
@property (nonatomic, strong) UIButton * btn_2;
@property (nonatomic, strong) UILabel * name_2;
@property (nonatomic, strong) UIImageView * image_2;
//评价
@property (nonatomic, strong) UIButton * btn_3;
@property (nonatomic, strong) UILabel * name_3;
@property (nonatomic, strong) UIImageView * image_3;

//
@property (nonatomic, strong) UICollectionView * collectionView_product;
//数据
@property (nonatomic, strong) NSMutableArray * arr_data;

@end

@implementation ChanpinliebiaoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self p_navi];
    
    [self p_setupView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - navi
- (void)p_navi
{
    _btnLeft.hidden = YES;
    _lblLeft.hidden = YES;
    _btnRight.hidden = YES;
    _lblRight.hidden = YES;
    _topView.backgroundColor = [UIColor blackColor];
    
    _lblTitle.text = @"产品列表";
    _lblTitle.textColor = [UIColor whiteColor];
    _lblTitle.font = [UIFont systemFontOfSize:19];
    
    UIImageView * image_return = [[UIImageView alloc] initWithFrame:CGRectMake(15, 32, 10, 20)];
    image_return.image = [UIImage imageNamed:@"zuofanhui@2x.png"];
    [_topView addSubview:image_return];
    
    UIButton * btn_return = [UIButton buttonWithType:(UIButtonTypeSystem)];
    btn_return.frame = CGRectMake(10, 20, 35, 44);
    [_topView addSubview:btn_return];
    [btn_return addTarget:self action:@selector(btn_returnAction:) forControlEvents:(UIControlEventTouchUpInside)];
}

//左返回
- (void )btn_returnAction:(UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

//显示tabbar
- (void )viewWillAppear:(BOOL)animated
{
    [(AppDelegate *)[[UIApplication sharedApplication] delegate] hiddenTabBar];
}

#pragma mark - 布局
- (void )p_setupView
{
    //ceshi
    self.arr_data = @[@"",@"",@"",@"",@""].mutableCopy;
    
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    //头视图
    [self p_headView];
    
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
    //每个item的大小
    int  item_length = (SCREEN_WIDTH - 30) / 2;
    layout.itemSize = CGSizeMake(item_length, item_length + 55);
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    self.collectionView_product = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64 + 50, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 50) collectionViewLayout:layout];
    self.collectionView_product.delegate = self;
    self.collectionView_product.dataSource = self;
    self.collectionView_product.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.view addSubview:self.collectionView_product];
    
    //注册
    [self.collectionView_product registerClass:[ChanpinliebiaoCollectionViewCell class] forCellWithReuseIdentifier:@"cell_product"];
}

#pragma mark - collectionView的代理
//有几个分区
- (NSInteger )numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
//每个分区有多少个
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.arr_data.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ChanpinliebiaoCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell_product" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    //
    [cell.image_product sd_setImageWithURL:[NSURL URLWithString:@""] placeholderImage:[UIImage imageNamed:@"cainixihuan@2x.png"]];
    
    //
    cell.btn_gouwuche.tag = indexPath.row + 100;
    [cell.btn_gouwuche addTarget:self action:@selector(btn_gouwucheAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    return cell;
}

- (void )collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//    NSLog(@"%ld",(long)indexPath.item);
    
    ChanpinxiangqingViewController * chanpinxiangqingViewController = [[ChanpinxiangqingViewController alloc] init];
    [self showViewController:chanpinxiangqingViewController sender:nil];
}

#pragma mark - 加入购物车
- (void )btn_gouwucheAction:(UIButton *)sender
{
    NSLog(@"加入购物车");
    
    NSInteger count = sender.tag - 100;
    NSLog(@"%ld",count);
}

#pragma mark - 销量
- (void )btn_1Action:(UIButton *)sender
{
    //    NSLog(@"销量");
    
    self.name_1.textColor = navi_bar_bg_color;
    self.image_1.image = [UIImage imageNamed:@"lusexiabiao@2x.png"];
    self.name_2.textColor = [UIColor blackColor];
    self.image_2.image = [UIImage imageNamed:@"huisexiaobiao@2x.png"];
    self.name_3.textColor = [UIColor blackColor];
    self.image_3.image = [UIImage imageNamed:@"huisexiaobiao@2x.png"];
    
    self.arr_data = @[@"",@"",@"",@"",@""].mutableCopy;
    
    dispatch_async(dispatch_get_main_queue(), ^{
        //刷新tableView(记住,要更新放在主线程中)
        
        [self.collectionView_product reloadData];
    });
}

#pragma mark - 价格
- (void )btn_2Action:(UIButton *)sender
{
    //    NSLog(@"价格");
    
    self.name_2.textColor = navi_bar_bg_color;
    self.image_2.image = [UIImage imageNamed:@"lusexiabiao@2x.png"];
    self.name_1.textColor = [UIColor blackColor];
    self.image_1.image = [UIImage imageNamed:@"huisexiaobiao@2x.png"];
    self.name_3.textColor = [UIColor blackColor];
    self.image_3.image = [UIImage imageNamed:@"huisexiaobiao@2x.png"];
    
    self.arr_data = @[@"",@""].mutableCopy;
    
    dispatch_async(dispatch_get_main_queue(), ^{
        //刷新tableView(记住,要更新放在主线程中)
        
        [self.collectionView_product reloadData];
    });
}

#pragma mark - 评价
- (void )btn_3Action:(UIButton *)sender
{
    //    NSLog(@"评价");
    
    self.name_3.textColor = navi_bar_bg_color;
    self.image_3.image = [UIImage imageNamed:@"lusexiabiao@2x.png"];
    self.name_1.textColor = [UIColor blackColor];
    self.image_1.image = [UIImage imageNamed:@"huisexiaobiao@2x.png"];
    self.name_2.textColor = [UIColor blackColor];
    self.image_2.image = [UIImage imageNamed:@"huisexiaobiao@2x.png"];
    
    self.arr_data = @[@"",@"",@"",@""].mutableCopy;
    
    dispatch_async(dispatch_get_main_queue(), ^{
        //刷新tableView(记住,要更新放在主线程中)
        
        [self.collectionView_product reloadData];
    });
}

#pragma mark - 头视图
- (void )p_headView
{
    self.view_headView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, 50)];
    self.view_headView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.view_headView];
    
    CGFloat length_x = SCREEN_WIDTH / 3;
    
    //销量
    self.name_1 = [[UILabel alloc] initWithFrame:CGRectMake(length_x / 2 - 20, 15, 35, 20)];
    self.name_1.textAlignment = NSTextAlignmentCenter;
    self.name_1.text = @"销量";
    self.name_1.font = [UIFont systemFontOfSize:16];
    self.name_1.textColor = navi_bar_bg_color;
    [self.view_headView addSubview:self.name_1];
    
    self.image_1 = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.name_1.frame) + 5, 23, 10, 5)];
    self.image_1.image = [UIImage imageNamed:@"lusexiabiao@2x.png"];
    [self.view_headView addSubview:self.image_1];
    
    self.btn_1 = [UIButton buttonWithType:(UIButtonTypeSystem)];
    self.btn_1.frame = CGRectMake(10, 0, length_x - 20, 50);
    [self.view_headView addSubview:self.btn_1];
    [self.btn_1 addTarget:self action:@selector(btn_1Action:) forControlEvents:(UIControlEventTouchUpInside)];
    
    //价格
    self.name_2 = [[UILabel alloc] initWithFrame:CGRectMake(length_x + length_x / 2 - 20, 15, 35, 20)];
    self.name_2.textAlignment = NSTextAlignmentCenter;
    self.name_2.text = @"价格";
    self.name_2.font = [UIFont systemFontOfSize:16];
    self.name_2.textColor = [UIColor blackColor];
    [self.view_headView addSubview:self.name_2];
    
    self.image_2 = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.name_2.frame) + 5, 23, 10, 5)];
    self.image_2.image = [UIImage imageNamed:@"huisexiaobiao@2x.png"];
    [self.view_headView addSubview:self.image_2];
    
    self.btn_2 = [UIButton buttonWithType:(UIButtonTypeSystem)];
    self.btn_2.frame = CGRectMake(length_x + 10, 0, length_x - 20, 50);
    [self.view_headView addSubview:self.btn_2];
    [self.btn_2 addTarget:self action:@selector(btn_2Action:) forControlEvents:(UIControlEventTouchUpInside)];
    
    //评价
    self.name_3 = [[UILabel alloc] initWithFrame:CGRectMake(length_x * 2 + length_x / 2 - 20, 15, 35, 20)];
    self.name_3.textAlignment = NSTextAlignmentCenter;
    self.name_3.text = @"评价";
    self.name_3.font = [UIFont systemFontOfSize:16];
    self.name_3.textColor = [UIColor blackColor];
    [self.view_headView addSubview:self.name_3];
    
    self.image_3 = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.name_3.frame) + 5, 23, 10, 5)];
    self.image_3.image = [UIImage imageNamed:@"huisexiaobiao@2x.png"];
    [self.view_headView addSubview:self.image_3];
    
    self.btn_3 = [UIButton buttonWithType:(UIButtonTypeSystem)];
    self.btn_3.frame = CGRectMake(length_x * 2, 0, length_x, 50);
    [self.view_headView addSubview:self.btn_3];
    [self.btn_3 addTarget:self action:@selector(btn_3Action:) forControlEvents:(UIControlEventTouchUpInside)];
}

#pragma mark - 懒加载
- (NSMutableArray * )arr_data
{
    if(_arr_data == nil)
    {
        self.arr_data = [NSMutableArray array];
    }
    
    return _arr_data;
}









@end
